#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <memory>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "Log.hpp"
#include "InetAddr.hpp"
namespace socket_ns
{
    const static int gbacklog = 8;
    class Socket;
    using socket_sptr = std::shared_ptr<Socket>;
    enum ERRor
    {
        USAGE_ERR = 1,
        SOCKET_ERR,
        BIND_ERR,
        LISTEN_ERR,
        CONN_ERR

    };
    class Socket // 模板方法模式
    {
    public:
        virtual void CreateSocketOrDie() = 0;
        virtual void BindOrDie(InetAddr &addr) = 0;
        virtual void ListenOrDie() = 0;
        virtual socket_sptr AcceptIt(InetAddr *client) = 0;
        virtual bool ConnectIt(InetAddr &server) = 0;
        virtual int returnfd() = 0;
        virtual int Recv(std::string*out)=0;
        virtual int Send(std::string what)=0;

    public:
        void BuildListenSocket(InetAddr &addr)
        {
            CreateSocketOrDie();
            BindOrDie(addr);
            ListenOrDie();
        }
        bool BuildConnectSocket(InetAddr &server)
        {
            CreateSocketOrDie();
            return ConnectIt(server);
        }

        // void BuildUdpServerSocket(InetAddr &addr)
        // {
        //     CreateSocketOrDie();
        //     BindOrDie(addr);
        // }
        // void BuildUdpClientSocket()
        // {
        //     CreateSocketOrDie();
        // }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int fd = -1) : _socket(fd)
        {
        }
        void CreateSocketOrDie() override
        {
            _socket = socket(AF_INET, SOCK_STREAM, 0);
            if (_socket < 0)
            {
                LOG(FATAL, "socket err");
                exit(SOCKET_ERR);
            }
            LOG(INFO, "socket success,socket is %d", _socket);
        }
        void BindOrDie(InetAddr &addr) override
        {
            struct sockaddr_in server = addr.Addr();
            int n = bind(_socket, (struct sockaddr *)&server, sizeof(server));
            if (n < 0)
            {
                LOG(FATAL, "bind err");
                exit(BIND_ERR);
            }
            LOG(INFO, "bind success");
        }
        void ListenOrDie() override
        {
            int n = listen(_socket, gbacklog);
            if (n < 0)
            {
                LOG(FATAL, "listen err");
                exit(LISTEN_ERR);
            }
            LOG(INFO, "listen success");
        }
        socket_sptr AcceptIt(InetAddr *client) override
        {
            struct sockaddr_in addr;
            socklen_t len = sizeof(addr);
            int fd = accept(_socket, (struct sockaddr *)&addr, &len);
            if (fd < 0)
            {
                LOG(WARNING, "accept err");
                return nullptr;
            }
            LOG(INFO, "accept success");

            *client = addr;
            socket_sptr ptr = std::make_shared<TcpSocket>(fd);
            return ptr;
        }
        bool ConnectIt(InetAddr &server) override
        {
            struct sockaddr_in tmp = server.Addr();
            int n = connect(_socket, (struct sockaddr *)&tmp, sizeof(tmp));
            if (n < 0)
            {
                LOG(FATAL, "connect err");
                return false;
            }
            LOG(INFO, "connect success");
            return true;
        }
        int returnfd() override
        {
            return _socket;
        }
         int Recv(std::string*out)override
         {
            char inbuffer[1024];
            ssize_t n=::recv(_socket,inbuffer,sizeof(inbuffer)-1,0);
            if(n>0)
            {
                *out+=inbuffer;
            }
            return n;
         }
        int Send(std::string what)override
        {
            int n=::send(_socket,what.c_str(),what.size(),0);
            return n;
        }


    private:
        int _socket;
    };



    // class UdpSocket : public TcpSocket
    // {
    // public:
    //     UdpSocket(int fd = -1) : _socket(fd)
    //     {
    //     }
    //     void CreateSocketOrDie() override
    //     {
    //         _socket = socket(AF_INET, SOCK_DGRAM, 0);
    //         if (_socket < 0)
    //         {
    //             LOG(FATAL, "socket err");
    //             exit(SOCKET_ERR);
    //         }
    //         LOG(INFO, "socket success,socket is %d", _socket);
    //     }
    //     void BindOrDie(InetAddr &addr) override
    //     {
    //         struct sockaddr_in server = addr.Addr();
    //         int n = bind(_socket, (struct sockaddr *)&server, sizeof(server));
    //         if (n < 0)
    //         {
    //             LOG(FATAL, "bind err");
    //             exit(BIND_ERR);
    //         }
    //         LOG(INFO, "bind success");
    //     }
    //     int returnfd() override
    //     {
    //         return _socket;
    //     }
    // private:
    // int _socket;
    // };
}